package com.neteast.androidclient.contact.activity;

import java.util.ArrayList;
import com.neteast.androidclient.contact.R;
import com.neteast.androidclient.contact.adapter.SearchResultAdapter;
import com.neteast.androidclient.contact.domain.Contact;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SearchActivity extends Activity implements OnClickListener {
	private LinearLayout fuzzyLayout;// 模糊搜索视图
	private LinearLayout exactLayout;// 精确搜索视图
	private Button btnBack;
	private Button btnfuzzy;
	private Button btnexact;

	private EditText fuzzyInput;
	private GridView resultGrid;
	private TextView noResultHint;
	private Button btnSmallSearch;
	private Button btnLargeSearch;

	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.search);
		findView();
		init();
	}

	private void findView() {
		fuzzyLayout = (LinearLayout) findViewById(R.id.fuzzySearchLayout);
		exactLayout = (LinearLayout) findViewById(R.id.exactSearchLayout);
		btnBack = (Button) findViewById(R.id.btnBack);
		btnfuzzy = (Button) findViewById(R.id.btnFuzzySearch);
		btnexact = (Button) findViewById(R.id.btnExactSearch);
		fuzzyInput = (EditText) findViewById(R.id.fuuzzyEdtInput);
		resultGrid = (GridView) findViewById(R.id.resultGrid);
		noResultHint = (TextView) findViewById(R.id.noResultHint);
		btnSmallSearch = (Button) findViewById(R.id.btnSmallSearch);
		btnLargeSearch = (Button) findViewById(R.id.btnLargeSearch);
		btnBack.setOnClickListener(this);
		btnfuzzy.setOnClickListener(this);
		btnexact.setOnClickListener(this);
		btnSmallSearch.setOnClickListener(this);
		btnLargeSearch.setOnClickListener(this);
	}

	private void init() {
		WindowManager wManager = getWindowManager();
		Display display = wManager.getDefaultDisplay();
		LayoutParams lParams = getWindow().getAttributes();
		lParams.height = display.getHeight() - 150;
		lParams.width = display.getWidth();
		getWindow().setAttributes(lParams);
		getWindow().setGravity(Gravity.BOTTOM);
		mContext = SearchActivity.this;
		setSearchView();
	}

	private void setSearchView() {
		
		ArrayList<Contact> list = new ArrayList<Contact>();
		Contact info = new Contact();
		for (int i = 0; i < 12; i++) {
			list.add(info);
		}
		SearchResultAdapter adapter = new SearchResultAdapter(mContext, list);
		resultGrid.setAdapter(adapter);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
		case R.id.btnFuzzySearch:
			fuzzyLayout.setVisibility(View.VISIBLE);
			exactLayout.setVisibility(View.GONE);
			v.setBackgroundResource(R.drawable.btn_search_fuzzy_select);
			btnexact.setBackgroundResource(R.drawable.btn_search_exact_normal);
			break;
		case R.id.btnExactSearch:
			exactLayout.setVisibility(View.VISIBLE);
			fuzzyLayout.setVisibility(View.GONE);
			v.setBackgroundResource(R.drawable.btn_search_exact_select);
			btnfuzzy.setBackgroundResource(R.drawable.btn_search_fuzzy_normal);
			break;
		case R.id.btnSmallSearch:
			String fuzzyKey = fuzzyInput.getText().toString().trim();
			if (!fuzzyKey.equals("")) {
				noResultHint.setText(getString(R.string.search_no_result_1,
						fuzzyKey));
			} else {
				Toast.makeText(mContext, "请输入模糊搜索关键字", Toast.LENGTH_SHORT)
						.show();
			}
			break;
		case R.id.btnLargeSearch:
			break;

		}

	}
}
